<?php
# get the install step
if (isset($_GET['step'])) {
    $step = $_GET['step'];
} else {
    $step = 0;
}
# get the install language
if (isset($_GET['lang'])) {
    $lang = $_GET['lang'];
} else {
    $lang = 'en';
}

# check is the language file is exists and readable.
if (is_readable('../languages/' . $lang . '_install.php')) {
    require_once '../languages/' . $lang . '_install.php';
}

require_once '../../arlicle/libs/functions.php';
require_once '../../arlicle/libs/plugin.php';
# get the arlicle directory
$base = str_replace('www/install/index.php', '', env('SCRIPT_NAME'));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Arlicle &rsaquo; <?php __e('Install'); ?></title>
    <link rel="stylesheet" href="<?php echo $base . 'arlicle/app/webroot/css/error.css'; ?>" type="text/css" />
</head>
<body id="error-page">
<h1 id="logo"><img alt="Arlicle" src="<?php echo $base . 'arlicle/app/webroot/img/logo_login.gif'; ?>" /></h1>
<?php



// Check if database.php has been created
if ($step != 3 and $step != 0) {
    if (file_exists('../config/database.php'))
        die(__("<p>The file 'www/config/database.php' already exists. If you need to reset any of the configuration items in this file, please delete it first. You may try <a href='../../index.php/admin/install/'>installing now</a>.</p>"));
}

if ($step != 0 && !is_writable('../')) {
    die(__('Sorry, I can&#8217;t write to the www directory. You&#8217;ll have to either change the permissions on your Arlicle "www" directory.') . '<p class="step"><a href="' . $base . 'www/install/index.php?step=1&lang=' . $lang . '" class="button">' . __('Next') . '</a></p>');
}

switch($step) {
    case 1:
?>
<p><?php __e('Welcome to Arlicle. Before getting started, we need some information on the database. You will need to know the following items before proceeding.'); ?></p>
<ol>
    <li><?php __e('Database name'); ?></li>

    <li><?php __e('Database username'); ?></li>
    <li><?php __e('Database password'); ?></li>
    <li><?php __e('Database host'); ?></li>
    <li><?php __e('Table prefix'); ?> (<?php __e('if you want to run more than one Arlicle in a single database'); ?>)</li>
</ol>

<p><?php __e('In all likelihood, these items were supplied to you by your ISP. If you do not have this information, then you will need to contact them before you can continue. If you&#8217;re all ready&hellip;'); ?></p>

<p class="step"><a href="index.php?step=2&lang=<?php echo $lang; ?>" class="button"><?php __e('Let&#8217;s go!'); ?></a></p>
<?php
        break;
    case 2:
?>
<form method="post" action="index.php?step=3&lang=<?php echo $lang; ?>">
    <p><?php __e('Below you should enter your database connection details. If you&#8217;re not sure about these, contact your host.'); ?></p>
    <table class="form-table">

        <tr>
            <th scope="row"><label for="dbname"><?php __e('Database name'); ?></label></th>
            <td><input name="dbname" id="dbname" type="text" size="25" value="arlicle" /></td>
            <td><?php __e('The name of the database you want to run Arlicle in.'); ?></td>
        </tr>
        <tr>
            <th scope="row"><label for="uname"><?php __e('Database username'); ?></label></th>

            <td><input name="uname" id="uname" type="text" size="25" value="username" /></td>
            <td><?php __e('Your MySQL username'); ?></td>
        </tr>
        <tr>
            <th scope="row"><label for="pwd"><?php __e('Database password'); ?></label></th>
            <td><input name="pwd" id="pwd" type="text" size="25" value="password" /></td>
            <td><?php __e('...and MySQL password.'); ?></td>

        </tr>
        <tr>
            <th scope="row"><label for="dbhost"><?php __e('Database host'); ?></label></th>
            <td><input name="dbhost" id="dbhost" type="text" size="25" value="localhost" /></td>
            <td><?php __e('99% chance you won&#8217;t need to change this value.'); ?></td>
        </tr>
        <tr>
            <th scope="row"><label for="prefix"><?php __e('Table prefix'); ?></label></th>

            <td><input name="prefix" id="prefix" type="text" id="prefix" value="al_" size="25" /></td>
            <td><?php __e('If you want to run multiple Arlicle installations in a single database, change this.'); ?></td>
        </tr>
    </table>
    <p class="step"><input type="submit" value="<?php __e('Submit'); ?>" class="button" /></p>
</form>
<?php
        break;
    case 3:
        if ($_POST) {
            $dbname  = trim($_POST['dbname']);
            $uname   = trim($_POST['uname']);
            $passwrd = trim($_POST['pwd']);
            $dbhost  = trim($_POST['dbhost']);
            $prefix  = trim($_POST['prefix']);
            if (empty($dbhost)) {
                $dbhost = 'localhost';
            }
            if (empty($prefix)) {
                $prefix = 'al_';
            }
            // Test the db connection.
            define('DB_NAME', $dbname);
            define('DB_USER', $uname);
            define('DB_PASSWORD', $passwrd);
            define('DB_HOST', $dbhost);
            require_once '../../arlicle/libs/model/aldb.php';
            if (!al_mkdir('../config')) {
                die("Sorry, I can't create the directory in www directory");
            }

            # Write the database config
            $handle = fopen('../config/database.php', 'w');
            $content = "<?php
// ** MySQL settings ** //
define('DB_NAME', '$dbname');    // The name of the database
define('DB_USER', '$uname');     // Your MySQL username
define('DB_PASSWORD', '$passwrd'); // ...and password
define('DB_HOST', '$dbhost');    // 99% chance you won't need to change this value
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('TABLE_PREFIX', '$prefix');
?>";
            fwrite($handle, $content);
            fclose($handle);
            
            # Write the admin setting
            $handle = fopen('../config/admin.php', 'w');
            $content = "<?php
// ** Admin settings ** //
define('DEFAULT_LANGUAGE', '$lang'); # The site default language
define('ARLICLE_ADMIN', 'admin'); # The site default directory
?>";
            fwrite($handle, $content);
            fclose($handle);
            chmod('../config/admin.php', 0666);
            
            # Write the cache config
            $handle = fopen('../config/cache.php', 'w');
            $content = "<?php
// ** Cache settings ** //
define('CACHE', false); # enable or off the cache
define('CACHE_PATH', TMP_PATH . 'cache/'); # set the cache path
define('CACHE_TIMEOUT', 60); # set the cache timeout

?>";
            fwrite($handle, $content);
            fclose($handle);
            chmod('../config/cache.php', 0666);
        }
?>
<p><?php echo __e('All right sparky! You&#8217;ve made it through this part of the installation. Arlicle can now communicate with your database. If you are ready, time now to&hellip;'); ?></p>

<p class="step"><a href="../../index.php/admin/install/" class="button"><?php echo __e('Run the install'); ?></a></p>
<?php
        break;
    case 0:
    default:
?>
<form method="get" action="index.php">
<p>Welcome to Arlicle. You need selected your default language.</p>

<p>
Default language: <select name="lang" id="lang">
    <option value="en">English</option>
    <option value="zh_cn">简体中文</option>
</select>
</p>
<input type="hidden" value="1" name="step">
<p class="step"><input type="submit" value=" Next > " class="button" /></p>
</form>
<?php
    break;
}
?>
    </body>
</html>
